package com.jwell.panprovider.ui.fragment.transportplaneinfo


import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.cargoapp.ui.base.MVPBaseFragment

import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.application.ConstValue
import com.jwell.panprovider.contract.WaybillContract
import com.jwell.panprovider.entity.ForExportWaybillListDto
import com.jwell.panprovider.entity.WaybillFatherBean
import com.jwell.panprovider.model.WaybillModel
import com.jwell.panprovider.presenter.WaybillPresenter
import com.jwell.panprovider.ui.adapter.WaybillAdapter
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*


/**
 * A simple [Fragment] subclass.
 * 供应详情-运单列表
 */
class WaybillFragment : MVPBaseFragment<WaybillPresenter>(), WaybillContract.View {

    private var searchData = WaybillModel.RequestData()
    private var planId = 0
    private var listData = ArrayList<ForExportWaybillListDto>()
    private var adapter = WaybillAdapter(listData)
    override fun createPresenter(): WaybillPresenter? {
        return WaybillPresenter()
    }


    private var data = ArrayList<WaybillFatherBean>()
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_waybill, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        activity?.intent?.extras?.let {
            planId = it.getInt("id")
        }
        initData()
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.VISIBLE
        searchData.planId = planId
        presenter?.getListData(searchData)
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
//        //搜索电子磅单
//        toolbar_right_image_serarh.setOnClickListener {
//            var intent = Intent(this, ElectronicPoundSearchActivity::class.java)
//            startActivity(intent)
//        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "暂无运单数据"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun showListData(data: List<ForExportWaybillListDto>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < ConstValue.MAXVUALUE) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}
